package com.cutthroatcreations.woodenrailway;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class UserProvider extends ContentProvider {

	SQLiteDatabase db;

	// All Static variables

	private static final String TAG = "DatabaseHandlerLog";

	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	public static final String DATABASE_NAME = "collectionManager";

	// Table names
	public static final String TABLE_COLLECTION = "collection";

	// Table Column names
	public static final String KEY_ID = "id";
	public static final String KEY_TRN_ID = "train_id";
	public static final String KEY_COL_YEAR = "year";
	public static final String KEY_USR_ID = "user_id";

	// Table Column Numbers
	public static final int ID_COLUMN = 0;
	public static final int TRN_ID_COLUMN = 1;
	public static final int COL_YEAR_COLUMN = 2;
	public static final int USR_ID_COLUMN = 3;

	// Content provider constants
	private static final String AUTH = "com.cutthroatcreations.woodenrailway.UserProvider";
	public static final Uri URI = Uri.parse("content://" + AUTH + "/"
			+ TABLE_COLLECTION);

	public static class trainDbHelper extends SQLiteOpenHelper {

		private static final String CREATE_TRAINS_TABLE = "CREATE TABLE "
				+ TABLE_COLLECTION + "(" + KEY_ID + " INTEGER PRIMARY KEY, "
				+ KEY_TRN_ID + " INTEGER, " + KEY_COL_YEAR + " VARCHAR(10), "
				+ KEY_USR_ID + " INTEGER, " + ")";

		public trainDbHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);

		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_TRAINS_TABLE);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			Log.v(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all data.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_COLLECTION);

		}
	}

	@Override
	public boolean onCreate() {

		Context context = getContext();
		trainDbHelper dbHelper = new trainDbHelper(context, DATABASE_NAME,
				null, DATABASE_VERSION);
		db = dbHelper.getWritableDatabase();
		return (db == null) ? false : true;

	}

	@Override
	public String getType(Uri uri) {
		return "vnd.android.cursor.dir/vnd.cutthroatcreations.woodenrailway";
	}

	@Override
	public int delete(Uri uri, String where, String[] args) {

		String[] whereSplit = where.split("\\.");
		Log.i(TAG, whereSplit[1]);
		String w = KEY_TRN_ID + "=" + whereSplit[0] + " AND " + KEY_COL_YEAR
				+ " = " + whereSplit[1];

		int count;
		try {
			count = db.delete(TABLE_COLLECTION, w, null);
			return count;
		} catch (SQLiteException e) {
			e.printStackTrace();
		}
		// getContext().getContentResolver().notifyChange(uri, null);
		return 0;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowID = db.insert(TABLE_COLLECTION, null, values);

		if (rowID > 0) {
			getContext().getContentResolver().notifyChange(uri, null);
			return uri;
		}
		throw new SQLException("Failed to insert row into " + uri);

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(TABLE_COLLECTION);
		String orderBy;
		if (TextUtils.isEmpty(sortOrder)) {
			orderBy = KEY_ID;
		} else {
			orderBy = sortOrder;
		}

		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, orderBy);

		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where,
			String[] whereArgs) {

		int count;

		count = db.update(TABLE_COLLECTION, values, where, whereArgs);

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

}
